package de.bht.fb6.mobisand.avoidlist.activities;

import org.apache.commons.validator.routines.EmailValidator;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import de.bht.fb6.mobisand.avoidlist.R;

/**
 * F??r den Login zust??ndige Activity.
 * 
 * @author Bruno Kirschner, Mario Doerwald
 * @since 04. Dez. 2012
 */
public class LoginActivity extends Activity {
	
	////////////////// Attribute //////////////////////////////////////////////////

	/** E-Mail TextFeld **/
	private EditText mail;
	
	/////////////////// Override Methoden /////////////////////////////////////////
	
	/**
	 * Validiert die eingegebene E-Mail Adresse.
	 * 
	 * @return
	 * 		Ist die eingegebene E-Mail Adresse valid (Apache Commons EMailValidator)
	 */
	public boolean validateMail() {
		Log.i(this.getClass().getName(), "### Mail:"+this.mail.getText().toString()+". ###");
		
		boolean ret = false;
		try {
			ret = EmailValidator.getInstance().isValid(this.mail.getText().toString());
		} catch (NoClassDefFoundError ex) {
			Log.i(this.getClass().getName(), "### Validation Fehler:"+ex.getMessage()+". ###");
		}
		
		return ret;
	}

	/////////////////// Override Methoden /////////////////////////////////////////
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		/** Referenz auf das E-Mail Feld **/
		this.mail = (EditText) this.findViewById(R.id.email);
		
		/** E-Mail Feld - Focus Listener **/
		mail.setOnFocusChangeListener(new OnFocusChangeListener () {
			
			@Override
			public void onFocusChange(View view, boolean focus) {
				
				final LoginActivity ctx = LoginActivity.this;
				
				if(focus == false) {
					Log.i(this.getClass().getName(), "### Focus: "+focus+". ###");
					
					if(!ctx.validateMail()) {
						Log.i(this.getClass().getName(), "### Falsche E-Mail. ###");

						AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
						builder.setTitle("Eingabe Ung??ltig")
							   .setMessage("Die eingebene E-Mail Adresse ist ung??ltig."+
										   "Bitte geben Sie eine Adresse nach folgendem Schema ein:"+
										   "'Benutzername@Domain.Kennung' ein.")
							   .setPositiveButton("Eingabe ??ndern",
									   new DialogInterface.OnClickListener() {
								   			public void onClick(DialogInterface dialog, int id) {
								   				ctx.mail.requestFocus();
								   			}
								   		});
						
						builder.create().show();
					}
				}
			}});

		/** Referenz auf das Passwortfeld **/
		final EditText password = (EditText) this.findViewById(R.id.password);
		password.setTransformationMethod(PasswordTransformationMethod.getInstance());
		
		/** Referenz auf den Login Button **/
		final Button login = (Button) this.findViewById(R.id.btn_login);
		login.setEnabled(false);
		
		/** f??ge einen onClickListener auf den Button hinzu **/
		login.setOnClickListener(new OnClickListener () {

			@Override
			public void onClick(View view) {
				/** Startet die Activity um eine Liste von ToAvoids anzuzeigen)**/
				startActivity(new Intent(LoginActivity.this, ListToAvoidActivity.class));
			}
		});
		
		/** Aktiviert den LoginButton falls alle Eingaben gemacht sind **/
		OnKeyListener enableLoginButton = new OnKeyListener () {

			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				
				if (password.getText().length() == 6 && LoginActivity.this.validateMail()) {
					login.setEnabled(true);
				} else if(login.isEnabled()){
					login.setEnabled(false);
				}
				return false;
			}
		};
		
		this.mail.setOnKeyListener(enableLoginButton);
		password.setOnKeyListener(enableLoginButton);
		
		Log.i(this.getClass().getName(), "### Login Activity Initialisiert. ###");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}
}
